package com.wqm.batis.common;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;

/**
 * @author wangqinmin
 * @date 2023/4/10 15:58
 * @description: 共产主义接班人
 */
@Getter
@Setter
@ToString
@ApiModel(value = "Responses对象", description = "统一返回参数包装类")
public class Responses<T> implements Serializable {

    private static final long serialVersionUID = 9176125179872407986L;

    @ApiModelProperty(value = "响应码(0失败 1成功)")
    private long code;

    @ApiModelProperty(value = "响应数据")
    private T data;

    @ApiModelProperty(value = "响应消息")
    private String msg;

    @ApiModelProperty(value = "详细响应码")
    private String subCode;

    @ApiModelProperty(value = "响应详细消息")
    private String subMsg;

    @ApiModelProperty(value = "响应时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date time = new Date();

    @ApiModelProperty(value = "链路追踪ID")
    private String traceId;


    /**
     * 该功能用于: 前端页面提示使用
     * <p>
     * 执行成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Responses<T> ok(T data) {
        ResponseEnum result = ResponseEnum.SUCCESS;
        Responses<T> responses = new Responses<>();
        responses.setData(data);
        responses.setCode(result.getCode());
        responses.setMsg(result.getMsg());
        return responses;
    }

    /**
     * 该功能用于: 前端页面提示使用
     * <p>
     * 操作失败
     *
     * @param msg
     * @param <T>
     * @return
     */
    public static <T> Responses<T> failed(String msg) {
        ResponseEnum result = ResponseEnum.FAILED;
        Responses responses = new Responses();
        responses.setData(null);
        responses.setCode(result.getCode());
        responses.setMsg(msg);
        return responses;
    }


    /**
     * 该功能用于: 业务异常、系统异常、某个微服务异常，等统一异常返回参数封装
     * <p>
     * 操作失败
     * --> 返回自定义失败状态码和消息
     *
     * @param errorCode
     * @param <T>
     * @return
     */
//    public static <T> Responses<T> failed(ErrorCode errorCode, String errorMsg) {
//        ResponseEnum failed = ResponseEnum.FAILED;
//        Responses<T> responses = new Responses<>();
//        responses.setCode(failed.getCode());
//        responses.setMsg(failed.getMsg());
//        responses.setSubCode(errorCode.getCode());
//        responses.setSubMsg(StringUtils.isBlank(errorCode.getMsg()) ? errorCode.getMsg() : errorMsg);
//        return responses;
//    }

    /**
     * 该功能用于灵活化的返回错误数据打印(一般用于全局异常处理,业务代码不建议使用)
     * 推荐使用 throw new BusinessException
     * <p>
     * 操作失败
     * --> 返回自定义失败状态码和消息
     *
     * @param errorCode
     * @param <T>
     * @return
     */
    public static <T> Responses<T> failed(String errorCode, String errorMsg) {
        Responses<T> responses = new Responses<>();
        ResponseEnum failed = ResponseEnum.FAILED;
        responses.setCode(failed.getCode());
        responses.setMsg(failed.getMsg());
        responses.setSubCode(errorCode);
        responses.setSubMsg(errorMsg);
        return responses;

    }
}
